﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
namespace MicroDal.Providers
{
    /// <summary>
    /// Public interface for custom client implementations
    /// </summary>
    public interface IDbResource : IDisposable
    {
        /// <summary>
        /// Executes the scalar.
        /// </summary>
        /// <param name="sql">The SQL.</param>
        /// <param name="commandType">Type of the command.</param>
        /// <param name="parameters">The parameters.</param>
        /// <returns></returns>
        object ExecuteScalar(string sql, CommandType commandType, IEnumerable<DbParameter> parameters);
        /// <summary>
        /// Executes the specified SQL.
        /// </summary>
        /// <param name="sql">The SQL.</param>
        /// <param name="commandType">Type of the command.</param>
        /// <param name="parameters">The parameters.</param>
        /// <returns></returns>
        int Execute(string sql, CommandType commandType, IEnumerable<DbParameter> parameters);
        /// <summary>
        /// Fetches the specified SQL.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">The SQL.</param>
        /// <param name="commandType">Type of the command.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="mapper">The mapper.</param>
        /// <returns></returns>
        IEnumerable<T> Fetch<T>(string sql, CommandType commandType, IEnumerable<DbParameter> parameters, Func<System.Data.Common.DbDataReader, T> mapper);
        /// <summary>
        /// Fetches the one.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">The SQL.</param>
        /// <param name="commandType">Type of the command.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="mapper">The mapper.</param>
        /// <returns></returns>
        T FetchOne<T>(string sql, CommandType commandType, IEnumerable<DbParameter> parameters, Func<System.Data.Common.DbDataReader, T> mapper);
        /// <summary>
        /// Gets the last inserted id.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        T GetLastInsertedId<T>();

        /// <summary>
        /// Gets the parameter initializer.
        /// </summary>
        /// <value>The parameter initializer.</value>
        Func<ParameterConfig, object, DbParameter> ParameterInitializer { get; }

        /// <summary>
        /// Returns the Regex pattern to match a parameter token. 
        /// </summary>
        string ParameterPattern { get; }

        /// <summary>
        /// Method for starting a transaction on the session. Use in using-construct and call Commit to commit changes. Dispose will rollback enlisted commands
        /// </summary>
        /// <returns></returns>
        DbTransaction GetTransaction(IsolationLevel isolationLevel);
    }
}
